package com.coreJava.xianchengchi;

import java.util.concurrent.*;

/**
 * 作者: LCG
 * 日期: 2020/2/10 22:25
 * 描述: 这个是并发包下的 Exectors的线程池
 */
public class TestXianChengChiImpl2 {
    public static void main(String[] args) throws InterruptedException {
        //创建一个可缓存的线程池，如果线程池长度超过处理需要，可灵活回收空闲线程，若无可回收，则新建线程。
        /*ExecutorService executorService = Executors.newCachedThreadPool();
        for (int i=0;i<100;i++){
            Thread thread = new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(2000);
                        System.out.println(Thread.currentThread().getName());
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });
            Thread.sleep(1000);
            executorService.submit(thread);
        }*/

        //创建一个定长的线程池
        //ExecutorService executorService = Executors.newFixedThreadPool(2);

        //创建一个周期线程池，支持定时及周期性任务执行。
        /*ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(2);
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                System.out.println("=========定时的执行===>"+Thread.currentThread().getName());
            }
        };
        scheduledExecutorService.scheduleWithFixedDelay(runnable,5,3,TimeUnit.SECONDS);*/
        //scheduledExecutorService.shutdown();

        //建立一个独立的线程池，利用该线程池可以实现线程的同步功能
        ExecutorService executorService1 = Executors.newSingleThreadExecutor();
        ExecutorService executorService = Executors.newFixedThreadPool(1);
        for (int j = 0; j < 5; j++) {
            final int h = j;
            Runnable runnable = new Runnable() {
                @Override
                public void run() {
                    for (int i = 0; i < 5; i++) {
                        System.out.println(h + "==========" + i);
                    }
                }
            };

            executorService.submit(runnable);

        }
    }

}
